package cn.com.ctcloud.saas.ids.demo.user.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import cn.com.ctcloud.saas.ids.demo.user.dao.IUserDemoDao;
import cn.com.ctcloud.saas.ids.demo.user.domain.UserDemoObj;

/**
 * Title: ChinaCT IDS
 * <p>
 * Description: Demo
 * <p>
 * Copyright: Copyright (c) 2008
 * <p>
 * Company: Liaoning Open Data Communication Co.,Ltd.
 *
 * @author lihui
 * @version 2.0.0
 */
@Repository
public class UserDemoDaoImpl implements IUserDemoDao {
	@Autowired
	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}

	/*
	 * 数据表排序
	 */
	private static final String TABLE_ORDER = "  ORDER BY model.NAME DESC ";

	/**
	 * 保存对象
	 *
	 * @param obj
	 *            对象
	 */
	@Override
	public void save(final UserDemoObj obj) {
		final StringBuffer sql = new StringBuffer("INSERT INTO " + UserDemoObj.TABLE_NAME + " (" + UserDemoObj.TABLE_ID
				+ ", " + UserDemoObj.TABLE_LOGIC_ID + ",  NAME, PWD, AGE, EMAIL) VALUES (?, ?,  ?, ?, ?, ?)");

		getJdbcTemplate().update(sql.toString(), new Object[] { obj.getId(), obj.getLogicId(), obj.getName(),
				obj.getPwd(), obj.getAge(), obj.getEmail() });
	}

	/**
	 * 修改对象
	 *
	 * @param obj
	 *            对象
	 */
	@Override
	public void update(final UserDemoObj obj) {
		final StringBuffer sql = new StringBuffer("UPDATE " + UserDemoObj.TABLE_NAME
				+ " SET  NAME = ?, PWD = ?, AGE = ?, EMAIL = ?  WHERE " + UserDemoObj.TABLE_LOGIC_ID + " = ?");

		getJdbcTemplate().update(sql.toString(),
				new Object[] { obj.getName(), obj.getPwd(), obj.getAge(), obj.getEmail(), obj.getLogicId() });
	}

	/**
	 * 删除对象
	 *
	 * @param logicId
	 *            对象ID
	 */
	@Override
	public void deleteByLogicId(final String logicId) {
		final String sql = "DELETE FROM " + UserDemoObj.TABLE_NAME + " WHERE " + UserDemoObj.TABLE_LOGIC_ID + " = ? ";

		getJdbcTemplate().update(sql, new Object[] { logicId });

	}

	/**
	 * 通过id查询信息
	 *
	 * @param logicId
	 *            主键
	 * @return 对象
	 */
	@Override
	public UserDemoObj getByLogicId(final String logicId) {
		final StringBuffer sql = new StringBuffer("SELECT " + makeField() + " FROM " + UserDemoObj.TABLE_NAME
				+ " model WHERE model." + UserDemoObj.TABLE_LOGIC_ID + " = ? " + UserDemoDaoImpl.TABLE_ORDER);
		final List<UserDemoObj> ret = getJdbcTemplate().query(sql.toString(), new Object[] { logicId },
				new RowMapper<UserDemoObj>() {
					public UserDemoObj mapRow(final ResultSet rs, final int arg1) throws SQLException {
						return setForm(rs);
					}
				});
		if (ret != null && !ret.isEmpty()) {
			return ret.get(0);
		} else {
			return null;
		}
	}

	/**
	 * 查询所有对象
	 *
	 * @return 对象集合
	 */
	@Override
	public List<UserDemoObj> findAll() {
		final StringBuffer sql = new StringBuffer(
				"SELECT " + makeField() + " FROM " + UserDemoObj.TABLE_NAME + " model " + UserDemoDaoImpl.TABLE_ORDER);

		return getJdbcTemplate().query(sql.toString(), new RowMapper<UserDemoObj>() {
			public UserDemoObj mapRow(final ResultSet rs, final int arg1) throws SQLException {
				return setForm(rs);
			}
		});
	}

	/**
	 * 用于查询的字段
	 *
	 * @return 字段
	 */
	private String makeField() {
		return "model." + UserDemoObj.TABLE_ID + " id, model." + UserDemoObj.TABLE_LOGIC_ID
				+ " logicId,  model.NAME modelName, model.PWD pwd, model.AGE age, model.EMAIL email ";
	}

	/**
	 * 取值oo
	 *
	 * @param rs
	 *            结果集
	 * @return 值
	 * @throws SQLException
	 *             Exception
	 */
	private UserDemoObj setForm(final ResultSet rs) throws SQLException {
		final UserDemoObj vo = new UserDemoObj();
		vo.setId(rs.getString("id"));
		vo.setLogicId(rs.getString("logicId"));
		vo.setName(rs.getString("modelName"));
		vo.setPwd(rs.getString("pwd"));
		vo.setAge(rs.getInt("age"));
		vo.setEmail(rs.getString("email"));
		return vo;
	}

}